package com.renwei.yunlong.activity.work;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fingdo.statelayout.StateLayout;
import com.google.gson.Gson;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.renwei.yunlong.R;
import com.renwei.yunlong.activity.news.ProblemInformationActivity;
import com.renwei.yunlong.adapter.DealAllAdapter;
import com.renwei.yunlong.base.BaseActivity;
import com.renwei.yunlong.bean.UpcomingBean;
import com.renwei.yunlong.utils.JsonMapUtil;
import com.renwei.yunlong.utils.ServiceRequestManager;
import com.renwei.yunlong.utils.StringUtils;
import com.zyf.fwms.commonlibrary.base.baseadapter.BaseRecyclerViewAdapter;

import java.util.HashMap;

/**
 * Project :  yunna.
 * Package name: com.renwei.yunlong.activity.work
 * Created by :  Benjamin.
 * Created time: 2018/1/8 15:43
 * Changed by :  Benjamin.
 * Changed time: 2018/1/8 15:43
 * Class description:工单搜索页面
 */

public class WorkSearchActivity extends BaseActivity implements XRecyclerView.LoadingListener, BaseRecyclerViewAdapter.OnItemClickLitener, View.OnClickListener, TextView.OnEditorActionListener {
    private XRecyclerView recyclerView;
    private EditText searchContent;
    private StateLayout stateLayout;
    private DealAllAdapter adapter;
    private int page = 1;
    private int mPosition = -1;
    private String upcoming = "0";


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_work_search);
        searchContent = findViewById(R.id.search_content);
        LinearLayout cancel = findViewById(R.id.search_top_cancel);
        stateLayout = findViewById(R.id.state_layout);
        recyclerView = findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setLoadingListener(this);
        cancel.setOnClickListener(this);
        searchContent.setOnEditorActionListener(this);
        adapter = new DealAllAdapter(this);
        recyclerView.setAdapter(adapter);
        adapter.setOnItemClickLitener(this);
        recyclerView.refresh();
    }


    /**
     * 初始化数据
     */
    private void initDeal() {
        HashMap<String, String> map = new HashMap<>();
        map.put("kind", "0");
        map.put("startDate", "");
        map.put("endDate", "");
        map.put("contractId", "");
        map.put("content", searchContent.getText().toString().trim());
        map.put("page", StringUtils.value(page));
        map.put("rows", 20 + "");
        if ("2".equals(companyType)) {
            map.put("currentUserId", serviceLoginBean.getRows().getEmployeeId());
            map.put("serviceProviderCode", serviceLoginBean.getRows().getCompany().getCompanyCode());
        } else if ("1".equals(companyType)) {
            map.put("currentUserId", ownerBean.getRows().getEmployeeId());
            map.put("ownerCode", ownerBean.getRows().getCompany().getCompanyCode());
        }
        ServiceRequestManager.getManager().getWorkList(this, "0",
                JsonMapUtil.mapToJson(map), this);
    }


    @Override
    public void onResume() {
        super.onResume();
        mPosition = -1;
    }

    @Override
    public void onSuccess(int requestId, String result) {
        recyclerView.loadMoreComplete();
        recyclerView.refreshComplete();
        UpcomingBean bean = new Gson().fromJson(result, UpcomingBean.class);
        if (bean.getMessage().getCode() == 200) {
            if (page == 1) {
                adapter.clean();
            }
            if (bean.getRows().size() < 20) {
                recyclerView.setLoadingMoreEnabled(false);
            }
            adapter.addAll(bean.getRows());
            if (adapter.getData().size() == 0) {
                stateLayout.showSearchEmptView();
            } else {
                stateLayout.showContentView();
            }
        }
    }


    public static void openActivity(Context context) {
        Intent intent = new Intent(context, WorkSearchActivity.class);
        context.startActivity(intent);
    }

    @Override
    public void onRefresh() {
        recyclerView.setLoadingMoreEnabled(true);
        page = 1;
        adapter.clean();
        initDeal();
    }

    @Override
    public void onLoadMore() {
        page++;
        initDeal();
    }

    @Override
    public void onItemClick(View view, int position) {
        mPosition = position;
        UpcomingBean.Rows bean = adapter.getData().get(position);
        if ("1".equals(bean.getKind())) {
            // 工单
            WorkInformationActivity.OpenActivity(this, bean.getRequestId() + "", bean.getServerRequestId() + "", bean.getOutsourceFlag());
        } else if ("2".equals(bean.getKind())) {
            ProblemInformationActivity.OpenActivity(this, bean.getRequestId() + "", bean.getMajorkey()
                    , bean.getOutsourceFlag());
        } else if ("3".equals(bean.getKind())) {
            ComplaintsInformationActivity.OpenActivity(this, bean.getRequestId() + "", bean.getNewRequestId() + ""
                    , bean.getServerRequestId() + "", bean.getNewServerRequestId(), bean.getOutsourceFlag(),
                    bean.getMajorkey() + "", bean.getSendUserName());
        } else if ("4".equals(bean.getKind())) {
            SpareInformationActivity.OpenActivity(this, bean.getRequestId() + "",
                    bean.getOutsourceFlag(), bean.getMajorkey());
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.search_top_cancel:
                this.finish();
                break;
            default:
                break;
        }
    }


    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_SEARCH) {
            // 当按了搜索之后关闭软键盘
            ((InputMethodManager) searchContent.getContext().getSystemService(
                    Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(
                    this.getCurrentFocus().getWindowToken(),
                    InputMethodManager.HIDE_NOT_ALWAYS);
            page = 1;
            initDeal();
            return true;
        }
        return false;
    }
}
